import { Sprite_Icon } from "@/sprites/Sprite_Icon.js";

export class Sprite_StateIcon extends Sprite_Icon {
    constructor() {
        super();
        this._battler = null;
        this._animationCount = 0;
        this._animationIndex = 0;
        this.anchor.x = 0.5;
        this.anchor.y = 0.5;
    }

    setup(battler) {
        this._battler = battler;
    }

    update() {
        super.update();
        ++this._animationCount;
        if (this._animationCount >= 40) {
            this.updateIcon();
            this._animationCount = 0;
        }
    }

    updateIcon() {
        let icons = [];
        if (this._battler && this._battler.isAlive()) {
            icons = this._battler.allIcons();
        }
        let index=0;
        if (icons.length > 0) {
            ++this._animationIndex;
            if (this._animationIndex >= icons.length) {
                this._animationIndex = 0;
            }
            index = icons[this._animationIndex];
        } else {
            this._animationIndex = 0;
            index = 0;
        }
        this.setIndex(index);
    }
}